//
//  HWXiaoXiMessageCenterViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/11/9.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWXiaoXiMessageCenterViewController.h"
#import "HWxiaoxiliebiaoTableViewCell.h"
#import "HWxiaoxizhongxingModel.h"
#import "HWtanchuView.h"

#import "HWXiaoXiCenzhongxingViewController.h"


@interface HWXiaoXiMessageCenterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *gameArr;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) HWtanchuView  *tankuangView;


@end

@implementation HWXiaoXiMessageCenterViewController

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.backgroundColor = RGB(247, 247, 247);
        
        
    
    }
    return   _mainaTableVie ;
}






- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {

    [super viewDidLoad];
    // yishiyongde   baisebeijingjilu

    [self setupUI];
    
    
    
    
       [self navigaidaohang];
    
    
    
}


- (void)navigaidaohang  {
    
    
    
    NSString  *biaoji = [HWjianzhiduiObject keylocalization:@"标记为已读"];
    
    
    UIBarButtonItem *but =   [[UIBarButtonItem alloc]initWithTitle:biaoji style:UIBarButtonItemStylePlain target:self action:@selector(shiyongjilu)];
 
    but.tintColor = RGB(68, 68, 68);
    self.navigationItem.rightBarButtonItem = but;
    
    
    
}

- (void)shiyongjilu {
    
          NSString *userID = [HWLocallyStoredData getLoginUserID];
             NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
             NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
             NSString *signMD5 = [WMUtil md5HexDigest:sign];
            NSString *typeStr =  @"1";
            NSDictionary *parametes = @{
                                            @"ct" :  @"app",
                                            @"ac" : @"message_read",
                                            @"ios" : @"1",
                                            @"uid": userID,
                                            @"t" :tStr,
                                            @"sign": signMD5,
                                            @"id": @"0",
                                            @"member_id":[HWLocallyStoredData getCode],
                                
                                            };
        
        [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
            
            // [SVProgressHUD dismiss];
            [self.mainaTableVie.mj_header endRefreshing];
            [self.mainaTableVie.mj_footer endRefreshing];
            
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

            if ([codeStr intValue] == 1) {
                
        
    //
             //    self.gameArr =   [HWxiaoxizhongxingModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
                
            [self.mainaTableVie.mj_header beginRefreshing];
               
       
             //    [self.mainaTableVie reloadData];
                
                
                
                
               
            }
            
            
            
            NSLog(@"%@",response);
            
            
        } failure:^(NSString *error) {
            [self.mainaTableVie.mj_header endRefreshing];
            [self.mainaTableVie.mj_footer endRefreshing];
            
            
        }];
    
    
    
}









- (void)setupUI {
    
    
  //   self.gameArr = [[NSMutableArray alloc]init];
    
    self.navigationItem.title  = @"消息中心";
    self.navigationItem.title = [HWjianzhiduiObject keylocalization:@"消息中心"];
    self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.mainaTableVie];
    [self addrefreshControl];
    
    
    
}





- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
    //  self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

#pragma  mark --------- 下拉刷新
- (void)underRefresh {
    
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        // 结束刷新
    //        [self.mainaTableVie.mj_header endRefreshing];
    //    });
    
    
    // [SVProgressHUD show];
    
   
    
    self.p = @"1";

       __weak typeof(self) weakSelf = self;
    
      self.gameArr = [[NSMutableArray alloc]init];
      
         NSString *userID = [HWLocallyStoredData getLoginUserID];
         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
         NSString *signMD5 = [WMUtil md5HexDigest:sign];
        NSString *typeStr =  @"1";
        NSDictionary *parametes = @{
                                        @"ct" :  @"app",
                                        @"ac" : @"message",
                                        @"ios" : @"1",
                                        @"uid": userID,
                                        @"t" :tStr,
                                        @"sign": signMD5,
                                        @"Version": @"3.0",
                                        @"member_id":[HWLocallyStoredData getCode],
                            
                                        };
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

        if ([codeStr intValue] == 1) {
            
    
//
            self.gameArr =   [HWxiaoxizhongxingModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
        
            [self.mainaTableVie reloadData];
            
            
            
            
           
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
}




- (void)onRefresh {
    
    
    if ([self.p  intValue] > [self.max_p intValue]) {
        
        [MBProgressHUD  showError:@"请稍后加载数据"];
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        return;
    }
    
    
    
     
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
       NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
       NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
       NSString *signMD5 = [WMUtil md5HexDigest:sign];
      NSString *typeStr =  @"3";
     NSDictionary *parametes = @{
                                           @"ct" :  @"app",
                                           @"ac" : @"mylb",
                                           @"ios" : @"1",
                                           @"uid": userID,
                                           @"t" :tStr,
                                           @"sign": signMD5,
                                           @"p" : self.p,
                                           @"type": typeStr,
                                           @"member_id":[HWLocallyStoredData getCode],
                                           };
    
    
    
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

        if ([codeStr intValue] == 1) {
            
         //    NSArray *arr1 =  response[@"data"][@"top"];

            NSArray *arr2 =  response[@"data"];
            
            
            
//
//             NSMutableArray *mutableArr2 =    [HWxingdeLBModel mj_objectArrayWithKeyValuesArray:arr2];
//
//
//
//
//            for (int  i = 0; i <  mutableArr2 .count ; i++) {
//
//                [self.gameArr addObject:mutableArr2[i]];
//
//
//            }
//
//
//
//
//            self.p = response[@"p"];
//            self.max_p = response[@"max_p"];
//
//            [self.mainaTableVie reloadData];
//
//
            
            
           
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
    
    
    
    
    
    
    
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {
    
    
    
     //  return 10;
    return  self.gameArr.count;
    
    
}

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    

    
    HWxiaoxiliebiaoTableViewCell   *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWxiaoxiliebiaoTableViewCell" owner:nil options:nil].firstObject;
        
        
        
    }
    HWxiaoxizhongxingModel *model = self.gameArr[indexPath.row];
    cell.xiaoxiLabel.text  = model.type;
    cell.riqiLabel.text = model.time;
    cell.leirongLabel.text = model.name;
    
    if ([model.read intValue] == 0) {
        cell.leirongLabel.textColor = RGB(68, 68, 68);
        
    }else {
        
         cell.leirongLabel.textColor = RGB(186, 186, 186);
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    UILongPressGestureRecognizer *gesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
       gesture.minimumPressDuration=1.0f;
       gesture.numberOfTouchesRequired = 1;
    //    gesture.delegate = self;
        cell.beijingView.userInteractionEnabled=YES;
       [cell.beijingView addGestureRecognizer:gesture];
    
    

    return  cell;
    
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return  78;
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"12333");
    
    HWxiaoxizhongxingModel   *mode = self.gameArr[indexPath.row];
    
    
    HWXiaoXiCenzhongxingViewController *xiaozhong = [[HWXiaoXiCenzhongxingViewController alloc]init];
    xiaozhong.model = mode;
    [self.navigationController pushViewController:xiaozhong animated:YES];
    
    
    
    
}





#pragma mark ---- 填加长安手势

- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
    
    UIView *view   =  paramSender.view;
    
    
    NSLog(@"%@",[[view superview] superview]);
    
     HWxiaoxiliebiaoTableViewCell *cell = (HWxiaoxiliebiaoTableViewCell *)[[view superview] superview];
    
     NSIndexPath *index =  [self.mainaTableVie  indexPathForCell:cell];
    
     HWxiaoxizhongxingModel *model =   self.gameArr[index.row];
    
      

    if (paramSender.state == UIGestureRecognizerStateBegan) {
        
           NSLog(@"long pressTap state :begin");
//        self.tankuangView  = [[HWtanchuView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
//        [kKeyWindow addSubview:self.tankuangView];
//
//
        
        NSString  *biaoji = [HWjianzhiduiObject keylocalization:@"标记或删除"];
        NSString  *biaojishenchu = [HWjianzhiduiObject keylocalization:@"标记已读或删除"];
        
        
         UIAlertController *alert = [UIAlertController alertControllerWithTitle:biaoji message:biaojishenchu preferredStyle:UIAlertControllerStyleActionSheet];
         
//              UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//                 //取消处理
//              }];
         
        NSString  *biaojidu = [HWjianzhiduiObject keylocalization:@"标记已读"];
             UIAlertAction *action2 = [UIAlertAction actionWithTitle:biaojidu style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                  //确认处理
                 
                 [self biaojiyidu:model];
                 
             }];
        
        NSString  *shanchu = [HWjianzhiduiObject keylocalization:@"删除"];
        
             UIAlertAction *action3 = [UIAlertAction actionWithTitle:shanchu style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                 
                          //确认处理
                 [self shachumodel:model];
                 
              }];
        
        NSString  *quxiao = [HWjianzhiduiObject keylocalization:@"取消"];
           UIAlertAction *action1 = [UIAlertAction actionWithTitle:quxiao style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                       //取消处理
            }];
               
        
            // [alert addAction:action1];
            [alert addAction:action2];
            [alert addAction:action3];
            [alert addAction:action1];
        
            [self.navigationController presentViewController:alert animated:YES completion:nil];
        
       
        
        

       }else {
           NSLog(@"long pressTap state :end");
       }

    NSLog(@"-------------");


}



// 标记已读
- (void)biaojiyidu:(HWxiaoxizhongxingModel *)model  {
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
                  NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
                  NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
                  NSString *signMD5 = [WMUtil md5HexDigest:sign];
                 NSString *typeStr =  @"1";
                 NSDictionary *parametes = @{
                                                 @"ct" :  @"app",
                                                 @"ac" : @"message_read",
                                                 @"ios" : @"1",
                                                 @"uid": userID,
                                                 @"t" :tStr,
                                                 @"sign": signMD5,
                                                 @"id": model.id,
                                                 @"member_id":[HWLocallyStoredData getCode],
                                     
                                                 };
             
             [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
                 
                 // [SVProgressHUD dismiss];
                 [self.mainaTableVie.mj_header endRefreshing];
                 [self.mainaTableVie.mj_footer endRefreshing];
                 
                 
                 NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

                 if ([codeStr intValue] == 1) {
                     
             
         //
                  //    self.gameArr =   [HWxiaoxizhongxingModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
                     
                 [self.mainaTableVie.mj_header beginRefreshing];
                    
            
                  //    [self.mainaTableVie reloadData];
                     
                     
                     
                     
                    
                 }
                 
                 
                 
                 NSLog(@"%@",response);
                 
                 
             } failure:^(NSString *error) {
                 [self.mainaTableVie.mj_header endRefreshing];
                 [self.mainaTableVie.mj_footer endRefreshing];
                 
                 
             }];
         
      
      
    
    
    
    
    
}




// 删除
- (void)shachumodel:(HWxiaoxizhongxingModel *)model  {
    
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    //  NSString *typeStr =  @"1";
           NSDictionary *parametes = @{
                                                  @"ct" :  @"app",
                                                  @"ac" : @"message_del",
                                                  @"ios" : @"1",
                                                  @"uid": userID,
                                                  @"t" :tStr,
                                                  @"sign": signMD5,
                                                  @"id": model.id,
                                                  @"Version": @"3.0",
                                                  @"member_id":[HWLocallyStoredData getCode],
                                      
                                                  };
              
              [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
                  
                  // [SVProgressHUD dismiss];
                  [self.mainaTableVie.mj_header endRefreshing];
                  [self.mainaTableVie.mj_footer endRefreshing];
                  
                  
                  NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

                  if ([codeStr intValue] == 1) {
                      
              
          //
                   //    self.gameArr =   [HWxiaoxizhongxingModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
                      
                  [self.mainaTableVie.mj_header beginRefreshing];
                     
             
                   //    [self.mainaTableVie reloadData];
                      
                      
                      
                      
                     
                  }
                  
                  
                  
                  NSLog(@"%@",response);
                  
                  
              } failure:^(NSString *error) {
                  [self.mainaTableVie.mj_header endRefreshing];
                  [self.mainaTableVie.mj_footer endRefreshing];
                  
                  
              }];
          
    
    
    
    
  
    
    



}





 
@end
